/**
 * 
 */
package com.ekingstar.hedss.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import oracle.sql.BLOB;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.util.HibernateCallbackUtil;
import com.ekingstar.framework.util.Page;
import com.ekingstar.hedss.domain.HedssZlfj;
import com.ekingstar.hedss.domain.HedssZllb;
import com.ekingstar.hedss.domain.HedssZlxx;
import com.ekingstar.hedss.service.IHedssService;

public class HedssServiceImpl implements IHedssService {
	private static Logger log = LoggerFactory.getLogger(HedssServiceImpl.class);
	private HibernateTemplate hibernateTemplate;
		
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	public HedssZlfj getZlfj(String id){
		HedssZlfj zlfj=(HedssZlfj)hibernateTemplate.get(HedssZlfj.class, id);
		return zlfj;
	}
	
	public boolean saveZlfj(HedssZlfj zlfj){
		hibernateTemplate.save(zlfj);
		return true;
	}
	
	public String saveFile(final File file,final String filename,final String zlxxId,final String username){   
		HedssZlfj zlfj=new HedssZlfj();
		zlfj.setName(filename);
		if(zlxxId!=null&&zlxxId.length()>0){
			HedssZlxx zlxx=getZlxx(zlxxId);
			zlfj.setTZlxx(zlxx);
		}
		
		FileInputStream fin = null;     
		try{
			try{			
			    fin=new FileInputStream(file);
			    byte[] data = new byte[(int)fin.available()]; 
			    fin.read(data); 
			    zlfj.setContent(data);
				fin.close();
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				if(fin!=null)
					fin.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	    
		zlfj.setAdderId(username);
		zlfj.setAddDate(new Date());
		
		saveZlfj(zlfj);
		
		return zlfj.getId();
	}
		
	public HedssZllb getZllb(String id){
		HedssZllb zllb=(HedssZllb)hibernateTemplate.get(HedssZllb.class, id);
		return zllb;
	}
	public boolean addZllb(HedssZllb zllb){
		hibernateTemplate.save(zllb);
		return true;
	}
	public boolean updateZllb(HedssZllb zllb){
		hibernateTemplate.update(zllb);
		return true;
	}
	public boolean deleteZllb(String id){
		HedssZllb zllb=getZllb(id);
		zllb.setDeleteDate(new Date());
		User user = UserUtil.getCurrentUser();
		zllb.setDeleterId(user.getName());
		hibernateTemplate.update(zllb);
		return true;
	}
	public HedssZllb viewZllb(String id){
		HedssZllb zllb=getZllb(id);		
		return zllb;
	}
	public Page getZllblist(HedssZllb zllb,int pageNo,int pageSize){
		if (pageNo <= 0) pageNo = 1;
		StringBuffer sb = new StringBuffer("" +
				" select a " +
				" from HedssZllb a " +
				" where a.deleteDate is null ");
		Map params = new HashMap();
		if (zllb != null){
			if (zllb.getName() != null && !zllb.getName().trim().equals("")){
				sb.append(" and (a.name like :name  )");
				params.put("name", "%"+zllb.getName().trim()+"%");
			}
		}
		sb.append(" order by a.name ");
		HibernateCallback action = HibernateCallbackUtil.getCallbackWithPage(sb, params, null, pageNo, pageSize);
		Page page = (Page)hibernateTemplate.execute(action);
		return page;
	}
	
	public List<HedssZlxx> getZlxxByZllbId(String zllbId){
		StringBuffer sb = new StringBuffer(" select new Map(a.id as id,a.title as title,a.TZllb.name as zllbname,a.addDate as addDate,a.adderId as adderId,a.lastUpdateDate as lastUpdateDate,a.updaterId as updaterId)" +
				" from HedssZlxx a where a.deleteDate is null and a.TZllb.id=:zllbId " +
				" order by a.title ");
		Map params = new HashMap();
		params.put("zllbId", zllbId);
		HibernateCallback action =  HibernateCallbackUtil.getCallback(sb, params, false, null);
		List list = (List)hibernateTemplate.execute(action);
		return list;
	}
	
	public HedssZlxx getZlxx(String id){
		HedssZlxx zlxx=(HedssZlxx)hibernateTemplate.get(HedssZlxx.class, id);
		return zlxx;
	}
	public boolean addZlxx(HedssZlxx zlxx){
		hibernateTemplate.save(zlxx);
		return true;
	}
	public boolean updateZlxx(HedssZlxx zlxx){
		hibernateTemplate.update(zlxx);
		return true;
	}
	public boolean deleteZlxx(String id){
		HedssZlxx zlxx=getZlxx(id);
		zlxx.setDeleteDate(new Date());
		User user = UserUtil.getCurrentUser();
		zlxx.setDeleterId(user.getName());
		hibernateTemplate.update(zlxx);
		return true;
	}
	public HedssZlxx viewZlxx(String id){
		HedssZlxx zlxx=getZlxx(id);
		HedssZllb zllb=getZllb(zlxx.getTZllb().getId());
		zlxx.setTZllb(zllb);
		return zlxx;
	}
	public Page getZlxxlist(HedssZlxx zlxx,int pageNo,int pageSize){
		if (pageNo <= 0) pageNo = 1;
		StringBuffer sb = new StringBuffer("" +
				" select new Map(a.id as id,a.title as title,a.TZllb.name as zllbname,a.addDate as addDate,a.adderId as adderId,a.lastUpdateDate as lastUpdateDate,a.updaterId as updaterId) " +
				" from HedssZlxx a " +
				" where a.deleteDate is null ");
		Map params = new HashMap();
		if (zlxx != null){
			if (zlxx.getTitle() != null && !zlxx.getTitle().trim().equals("")){
				sb.append(" and (a.title like :name  )");
				params.put("name", "%"+zlxx.getTitle().trim()+"%");
			}
			if (zlxx.getTZllb() != null && !zlxx.getTZllb().getId().trim().equals("")){
				sb.append(" and (a.TZllb.id=:zllbId )");
				params.put("zllbId",zlxx.getTZllb().getId().trim());
			}
		}
		sb.append(" order by a.TZllb.name,a.title ");
		HibernateCallback action = HibernateCallbackUtil.getCallbackWithPage(sb, params, null, pageNo, pageSize);
		Page page = (Page)hibernateTemplate.execute(action);
		return page;
	}
	
	public List<HedssZlfj> getZlfjByZlxxId(String zlxxId){
		StringBuffer sb = new StringBuffer(" select new Map(a.id as id,a.name as name,a.addDate as addDate,a.adderId as adderId) " +
				" from HedssZlfj a where a.deleteDate is null and a.TZlxx.id=:zlxxId ");
		Map params = new HashMap();
		params.put("zlxxId", zlxxId);
		HibernateCallback action =  HibernateCallbackUtil.getCallback(sb, params, false,null);
		List list = (List)hibernateTemplate.execute(action);
		return list;
	}
	
	public boolean deleteZlfj(String id){
		HedssZlfj zlfj=getZlfj(id);
		hibernateTemplate.delete(zlfj);
		return true;
	}

}
